package org.devgframwork.datasync.core.support.service;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.devgframwork.datasync.core.support.DataSourceMemory;
import org.devgframwork.datasync.core.utils.CommonUtil;

/**
 * @Description
 * @auther dh
 * @date 2022/1/5 18:28
 */
@Slf4j
public class MessageService {

    public static void sendRabbitMqMessage(String connectionName, String routingKey, String message) {
        try {
            if (CommonUtil.isNullOrEmpty(connectionName)) {
                log.error("消息发送失败,未初始化 connectionName...");
                return;
            }
            Channel channel = DataSourceMemory.connectionChannelMap.get(connectionName);
            if (null == channel) {
                log.error("消息发送失败,未初始化channel...");
                return;
            }
            String changeName = connectionName + CommonUtil.suffixExchange;
            channel.basicPublish(changeName, routingKey, null, message.getBytes(CommonUtil.charset));
        } catch (Exception ex) {
            ex.printStackTrace();
            log.error("" + ex.getMessage());
        }
    }
}
